<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/27
 * Time: 19:16
 */
require_once __DIR__.'/snoopy/Snoopy.php';
require_once __DIR__.'/pquery/phpQuery.php';
class Http
{
    private $snoopy = null;
    const COOKIE_SESS_NAME="cookies";

    public function __construct()
    {
        if(empty($this->session(self::COOKIE_SESS_NAME)))
            $this->session(self::COOKIE_SESS_NAME,[]);
        $this->snoopy = new Snoopy();
    }

    //操作session
    private function session()
    {
        if(session_status()==PHP_SESSION_NONE ||session_status()==PHP_SESSION_NONE)
            session_start();
        $func_num = func_num_args();
        if($func_num<1)
            throw new Exception("参数错误!");
        if($func_num==1)
        {
            $session_name = func_get_arg(0);
            $key_exists = array_key_exists($session_name,$_SESSION);
            return $key_exists?$_SESSION[$session_name]:null;
        }
        if($func_num==2)
        {
            $session_name = func_get_arg(0);
            $session_value = func_get_arg(1);
            $_SESSION[$session_name]=$session_value;
           return $_SESSION[$session_name];
        }
    }

    //获取cookie
    public function getCookies()
    {
        return $this->session(self::COOKIE_SESS_NAME);
    }

    //清除所有cookie
    public function clearCookies()
    {
        $this->session(self::COOKIE_SESS_NAME,null);
    }

    //抓取表单内容  返回key-value数组形式
    public function fetchForm($url,$filter="input,select")
    {
       @$form_html_str = $this->snoopy->fetchform($url)->results;
       phpQuery::newDocument($form_html_str);
       $inputs = pq($filter);
       $form_key_values = [];
       foreach ($inputs as $input)
       {
          $name = pq($input)->attr("name");
          $value = pq($input)->attr("value");
           $form_key_values[$name]=$value;
       }
       $info['url']=$this->snoopy->lastredirectaddr;
       $info['form_data']=$form_key_values;
       return $info;
    }

    //抓取连接link
    public function fetchLinks($url)
    {
       $res=  $this->snoopy->fetchlinks($url);
       return $res->results;
    }

    //抓取页面
    public function get($url)
    {
        $this->setCookies();//设置cookies
        $resp = $this->snoopy->fetch($url); //抓取页面
        $this->storeCookies(); //存储cookies
        return $resp->results;
    }

    //提交post表单
    public function post($url,$post_data)
    {
        $this->setCookies();//设置cookies
        $this->snoopy->referer=$url;
        $res = $this->snoopy->submit($url,$post_data);
        $this->storeCookies(); //存储cookies
        return $res;
    }

    //存储cookies到session里面
    private function storeCookies()
    {
        $this->snoopy->setcookies();
        $cookies = $this->session(self::COOKIE_SESS_NAME);
        $cookies = array_merge($cookies,$this->snoopy->cookies);
        $this->session(self::COOKIE_SESS_NAME,$cookies);
    }

    //设置每次发送cookies
    private function setCookies()
    {
        $this->snoopy->cookies=$this->session(self::COOKIE_SESS_NAME);
    }
}